package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Role;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.system.RoleService;

import java.util.List;
import java.util.UUID;

public class RoleServiceImpl implements RoleService {
    @Override
    public PageInfo<Role> findAll(String sizeStr, String pageStr) {
        int page = 1;
        try {
            page = Integer.parseInt(pageStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        int size = 5;
        try {
            size = Integer.parseInt(sizeStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        //设置分页助手
        PageHelper.startPage(page, size);
        RoleDao dao = DaoInstanceFactory.getMapper(RoleDao.class);
        List<Role> list = dao.findAll();
        PageInfo<Role> role = new PageInfo<>(list);
        return role;
    }

    @Override
    public void save(Role role) {
        RoleDao dao = DaoInstanceFactory.getMapper(RoleDao.class);
        String id = UUID.randomUUID().toString().replace("-", "");
        role.setId(id);
        dao.save(role);

    }

    @Override
    public List<Role> findAll() {
        RoleDao mapper = DaoInstanceFactory.getMapper(RoleDao.class);
        List<Role> list = mapper.findAll();
        return list;
    }

    @Override
    public Role findById(String id) {
        RoleDao dao = DaoInstanceFactory.getMapper(RoleDao.class);
        Role role = dao.findById(id);
        return role;
    }

    @Override
    public void update(Role role) {
        RoleDao dao = DaoInstanceFactory.getMapper(RoleDao.class);
        dao.update(role);
    }

    @Override
    public void delete(String id) {
        RoleDao dao = DaoInstanceFactory.getMapper(RoleDao.class);
        dao.delete(id);
    }
}
